package org.example.dao;

import org.example.model.Channel;
import org.example.util.DBUtil;
import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ChannelDao {

    public static List<Channel> selectAll() {
        Connection c=null;
        Statement ps=null;
        ResultSet rs=null;
        try {
            c= DBUtil.getConnection();
            String sql="select * from channel";
            ps=c.createStatement();
            rs=ps.executeQuery(sql);
            List<Channel> channels=new ArrayList<>();
            while (rs.next()){
                 //每一个数据转为Channel对象
                Channel channel=new Channel();
                int id=rs.getInt("id");
                String name=rs.getString("name");
                channel.setId(id);
                channel.setName(name);
                channels.add(channel);
            }
            return channels;
        }catch (SQLException e){
            throw  new RuntimeException("查询频道列表JDBC出错",e);
        }finally {
          DBUtil.close(c,ps,rs);
        }
    }
    @Test
    public void  selectAllTest(){
        System.out.println(selectAll());
    }
}
